<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2014/10/11
 * Time: 15:18
 */
class FrontController extends Controller{
    protected $domain = '';
    protected $bank_id = 0;
    protected $network_id = 0;//网点ID
    protected $network_name = '';//网点名称
    protected $level = 0;//等级
    protected $auth_grade = 0;//权限等级
    protected $up_network_id = 0;//上级网点ID
    protected $username = '';//登陆用户名
    protected $open_staff = 0;//是否开通排班
    /**
     * 初始化,进来的时候要判断域名
     * 根据域名判断这个客户
     */
    public function init(){
        parent::init();
        $this->check_login();
    }
    private function check_login(){
        $this->bank_id = Yii::app()->session['bank_id'];
        $this->network_id = (int)Yii::app()->session['network_id'];
        $this->network_name = Yii::app()->session['network_name'];
        $this->level = (int)Yii::app()->session['level'];
        $this->auth_grade = (int)Yii::app()->session['auth_grade'];
        $this->up_network_id = (int)Yii::app()->session['up_network_id'];
        $this->username = Yii::app()->session['network_username'];
        $this->open_staff = Yii::app()->session['open_staff'];
        if (empty($this->network_id)) {
            $this->jump('请登陆后操作','/login/index');
        }
        $this->smarty->assign('open_staff', $this->open_staff);
        $this->smarty->assign('network_name', $this->network_name);
        $this->smarty->assign('username', $this->username);
        $this->smarty->assign('open_report', (int)Yii::app()->session['open_report']);
        //网点等级，第四级即网点级别的只有对标权，其他级别的有项目权限
        $this->smarty->assign('level', $this->auth_grade);
        $controller = strtolower($this->getId());
        $this->smarty->assign('controller', $controller);
        //银行信息
        $bank_info = NetworkBank::model()->findByPk($this->bank_id)->attributes;
        $this->smarty->assign('bank_info', $bank_info);
    }
    /**
     * js跳页
     */
    protected function jump($msg, $url=''){
        if ($url) {
            echo '<script>alert("'.$msg.'");window.location.href="'.Yii::app()->createUrl($url).'"</script>';exit;
        } else {//没有返回上一级
            echo '<script>alert("'.$msg.'");history.go(-1);</script>';exit;
        }
    }
    protected $ajaxreturn = array();
    protected function _ajax($arr=array()){
        if ($arr) {
            $this->ajaxreturn = $arr;
        }
        if ($_GET['callback']) {
            echo $_GET['callback'] . '(' .CJSON::encode($this->ajaxreturn).')';
        } else {
            echo CJSON::encode($this->ajaxreturn);
        }
        exit;
    }
    /**
     * 设置ajaxreturn
     */
    protected function setreturn($status=0, $note='', $data=array()){
        $this->ajaxreturn = array('status' => $status,'note' => $note, 'data' => $data);
    }
    /**
     * 具体网点，直接查找网点的问卷
     * 得到项目，问卷，网点
     */
    public function level4($question_id=0){
        $networkquestion = new NetworkQuestion();
        $question = new QuestionBase();
        $project = new Project();
        $network = new NetWork();
        //问卷列表
        $question_list = array();
        //项目列表
        $project_list = array();
        //项目问卷列表
        $project_question = array();
        //网点问卷列表
        $netquestion = array();
        //得到此银行的项目，需要用项目ID限制哪些网点
        $projects = Project::model()->findAll('bank_id='.intval($this->bank_id).' and status='.Project::PROJECT_RUNNING);
        $project_ids = array();
        if ($projects) {
            foreach ($projects as $key => $val) {
                $project_ids[] = $val['project_id'];
            }
        }

        $criteria = new CDbCriteria();
        $criteria->addColumnCondition(array('network_id' => $this->network_id));
        if ($question_id) {
            $criteria->addColumnCondition(array('question_id' => $question_id));
        }
 
        if ($project_ids) {
            $criteria->addInCondition('project_id', $project_ids);
        }
        //$criteria->addInCondition('status', array(NetworkQuestion::NETWORK_QUESTION_COMPLETE,  NetworkQuestion::NETWORK_QUESTION_NORMAL));
        $criteria->addColumnCondition(array('status' => NetworkQuestion::NETWORK_QUESTION_NORMAL));
        $criteria->order = 'audit desc';
        $netquestion = $networkquestion->findAll($criteria);
        
        if ($netquestion) {
            foreach ($netquestion as $key => $val) {
                $question_info = $question->findByPk($val['question_id'])->attributes;
                if ($question_info['status']==2) {//只显示发布状态的
                    $question_list[$val['question_id']] = $question_info; 
                
                    $project_info = $project->findByPk($val['project_id'])->attributes;
                    $project_list[$val['project_id']] = $project_info;
                    $project_question[$val['project_id']][] = $question_info;
                } else {
                    unset($netquestion[$key]);
                }
            }
        } else {
            return false;
        }
 
        return array('question_list'=>$question_list,'project_list' => $project_list,'netquestion' => $netquestion,'project_question'=>$project_question);
    }
    /**
     * 总行级别
     * 得到项目，问卷，网点
     */
    public function level1($question_id=0){
        $networkquestion = new NetworkQuestion();
        $question = new QuestionBase();
        $project = new Project();
        $network = new NetWork();
        //问卷列表
        $question_list = array();
        //项目列表
        $project_list = array();
        //项目问卷列表
        $project_question = array();
        //网点问卷列表
        $netquestion = array();
        //网点问卷里的网点名称,总行审核所有时用
        $netquestion_name = array();
        
        $netwrok_ids = $project_ids = $question_ids = array();
        //得到下级网点,分行、支行
        $network_list = $network->findAll("bank_id={$this->bank_id} and auth_grade in (2,3,4)");
        $network_ids[0] = $this->network_id;
        //得到下级网点
        if ($network_list) {
            foreach ($network_list as $key => $val) {
                $network_ids[] = $val['network_id'];
            }
            //得到网点问卷，里面有项目和问卷
            $criteria = new CDbCriteria();
            $criteria->addInCondition('network_id', $network_ids);
            //$criteria->addInCondition('status', array(NetworkQuestion::NETWORK_QUESTION_COMPLETE,  NetworkQuestion::NETWORK_QUESTION_NORMAL));
            $criteria->addColumnCondition(array('status' => NetworkQuestion::NETWORK_QUESTION_NORMAL));
            $criteria->order = 'audit desc';
            $netquestion = $networkquestion->findAll($criteria);
            foreach ($netquestion as $key => $val) {
                $project_ids[] = $val['project_id'];
                $question_ids[] = $val['question_id'];
                $netquestion_name[$val['nq_id']] = $val['network_name'];
            }
        }
		
        $network_ids = array_unique($network_ids);
        $project_ids = array_unique($project_ids);
        $question_ids = array_unique($question_ids);
    
        if (empty($project_ids) || empty($question_ids)) {
                return false;
        }
		
        $project_list = $project->findAll('project_id in ('.join(',',$project_ids).') and status='.Project::PROJECT_RUNNING);
        $projectlist = array();
        foreach ($project_list as $key => $val) {
                $project_id = $val['project_id'];
                $projectlist[$project_id] = $val;
        }

        $question_list = $question->findAll('question_id in ('.join(',', $question_ids).') and status='.QuestionBase::QUESTION_RELEASE);
        $questionlist = array();
        if ($question_list) {
            foreach ($question_list as $key => $val) {
                if ($val['status'] == QuestionBase::QUESTION_RELEASE) {
                    $questionlist[$val['question_id']] = $val;
                    $project_question[$val['project_id']][] = $val;
                }
                unset($question_list[$key]);
            }
        }

        return array('question_list'=>$questionlist,'project_list' => $projectlist,'netquestion' => $netquestion,
            'project_question'=>$project_question,'netquestion_name' => $netquestion_name);
    }
    /**
     * 分行级别，得到本行及下级支行
     * 得到本行的项目，问卷，及具体网点
     */
    public function level2($question_id=0){
        $networkquestion = new NetworkQuestion();
        $question = new QuestionBase();
        $project = new Project();
        $network = new NetWork();
        //问卷列表
        $question_list = array();
        //项目列表
        $project_list = array();
        //网点问卷列表
        $netquestion = array();
        //项目下的问卷
        $project_question = array();
        $netwrok_ids = $project_ids = $question_ids = array();
        
        //得到下级网点
        $network_list = $network->getAllNext($this->network_id, 2);
        $network_ids[0] = $this->network_id;
        //得到下级网点
        if ($network_list) {
            foreach ($network_list as $key => $val) {
                $network_ids[] = $val['network_id'];
            }

            $network_ids = array_unique($network_ids);
            //得到网点问卷，里面有项目和问卷
            $criteria = new CDbCriteria();
            $criteria->addInCondition('network_id', $network_ids);
            //$criteria->addInCondition('status', array(NetworkQuestion::NETWORK_QUESTION_COMPLETE,  NetworkQuestion::NETWORK_QUESTION_NORMAL));
            $criteria->addColumnCondition(array('status' => NetworkQuestion::NETWORK_QUESTION_NORMAL));
            $criteria->order = 'audit desc';
            $netquestion = $networkquestion->findAll($criteria);
            foreach ($netquestion as $key => $val) {
                $project_ids[] = $val['project_id'];
                $question_ids[] = $val['question_id'];
            }
        }
        
        $project_ids = array_unique($project_ids);
        $question_ids = array_unique($question_ids);
		if (empty($project_ids) || empty($question_ids)) {
			return false;
		}
        
		if ($project_ids) {
			$project_list = $project->findAll('project_id in ('.join(',',$project_ids).') and status='.Project::PROJECT_RUNNING);

			$projectlist = array();
			foreach ($project_list as $key => $val) {
				$project_id = $val['project_id'];
				$projectlist[$project_id] = $val;
			}
		}

		
        $question_list = $question->findAll('question_id in ('.join(',', $question_ids).') and status='.QuestionBase::QUESTION_RELEASE);
        $questionlist = array();
	foreach ($question_list as $key => $val) {
            if ($val['status'] == QuestionBase::QUESTION_RELEASE) {
                $questionlist[$val['question_id']] = $val;
                $project_question[$val['project_id']][] = $val;
            }
            unset($question_list[$key]);
        }

        return array('question_list'=>$questionlist,'project_list' => $projectlist,'netquestion' => $netquestion,'project_question'=>$project_question);
    }
    /**
     * 支行级别
     * 
     */
    public function level3($question_id=0){
        $networkquestion = new NetworkQuestion();
        $question = new QuestionBase();
        $project = new Project();
        $network = new NetWork();
        //问卷列表
        $question_list = array();
        //项目列表
        $project_list = array();
        //项目问卷列表
        $project_question = array();
        
        $netwrok_ids = $project_ids = $question_ids = array();
        
        //得到下级网点
        $network_list = $network->findAll("up_network_id={$this->network_id} and auth_grade=4");
        
        $network_ids[0] = $this->network_id;
        //得到下级网点
        if ($network_list) {
            foreach ($network_list as $key => $val) {
                $network_ids[] = $val['network_id'];
            }
            //得到网点问卷，里面有项目和问卷
            $criteria = new CDbCriteria();
            $criteria->addInCondition('network_id', $network_ids);
            //$criteria->addInCondition('status', array(NetworkQuestion::NETWORK_QUESTION_COMPLETE,  NetworkQuestion::NETWORK_QUESTION_NORMAL));
            $criteria->addColumnCondition(array('status' => NetworkQuestion::NETWORK_QUESTION_NORMAL));
            $criteria->order = 'audit desc';
            $netquestion = $networkquestion->findAll($criteria);
            foreach ($netquestion as $key => $val) {
                $project_ids[] = $val['project_id'];
                $question_ids[] = $val['question_id'];
            }
        }
        $network_ids = array_unique($network_ids);
        $project_ids = array_unique($project_ids);
        $question_ids = array_unique($question_ids);
        
		if (empty($project_ids) || empty($question_ids)) {
			return false;
		}
		
        $project_list = $project->findAll('project_id in ('.join(',',$project_ids).') and status='.Project::PROJECT_RUNNING);

		$projectlist = array();
		foreach ($project_list as $key => $val) {
			$project_id = $val['project_id'];
			$projectlist[$project_id] = $val;
		}
		
        $question_list = $question->findAll('question_id in ('.join(',', $question_ids).') and status='.QuestionBase::QUESTION_RELEASE);
        
	$questionlist = array();
	foreach ($question_list as $key => $val) {
            if ($val['status'] == QuestionBase::QUESTION_RELEASE) {
                $questionlist[$val['question_id']] = $val;
                $project_question[$val['project_id']][] = $val;
            }
            unset($question_list[$key]);
        }
		
        return array('question_list'=>$questionlist,'project_list' => $projectlist,'netquestion' => $netquestion,'project_question'=>$project_question);
    }
    /**
     * 我的上一级，或者下一级网点的等级
     * 可能没有二级或者三级网点
     * @param $isup true上级 false下级
     */
    protected function myuplevel($isup=true)
    {
        if ($isup == true) {
            if ($this->auth_grade ==1) {
                return 1;
            }
            $info = NetWork::model()->findByPk($this->up_network_id);
        } else {
            $info = NetWork::model()->find('up_network_id='.$this->network_id);
        }
        
        return intval($info->auth_grade);
    }
}